package com.xszx.dao.impl;

import com.xszx.bean.Address;
import com.xszx.dao.AddressDao;
import com.xszx.util.JDBCUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class AddressDaoImpl implements AddressDao {
    @Override
    public void addAddress(Address address) {
        try {
            Connection connection = JDBCUtil.getConnection();
            String sql = "insert into t_address values(null, ?, ?, ?, ?, ?, 0, 0)";
            PreparedStatement ps = connection.prepareStatement(sql);
            ps.setInt(1, address.getUserId());
            ps.setString(2, address.getAddress());
            ps.setInt(3, address.getPostalCode());
            ps.setString(4, address.getConName());
            ps.setString(5, address.getConPhone());
            ps.execute();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void updateAddress(Address address) {
        try {
            Connection connection = JDBCUtil.getConnection();
            String sql = "update t_address set address = ?, postalcode = ?, conname = ?, conphone = ? where id = ?";
            PreparedStatement ps = connection.prepareStatement(sql);
            ps.setString(1, address.getAddress());
            ps.setInt(2, address.getPostalCode());
            ps.setString(3, address.getConName());
            ps.setString(4, address.getConPhone());
            ps.setInt(5, address.getId());
            ps.execute();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void deleteAddress(Integer id) {
        try {
            Connection connection = JDBCUtil.getConnection();
//            String sql = "delete from t_address where id = ?";
            String sql = "update t_address set del = 1 where id = ?";
            PreparedStatement ps = connection.prepareStatement(sql);
            ps.setInt(1, id);
            ps.execute();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    @Override
    public List<Address> getAllAddress(Integer userId) {
        try {
            Connection connection = JDBCUtil.getConnection();
            String sql = "select * from t_address where user_id = ? and del = 0 order by state desc";
            PreparedStatement ps = connection.prepareStatement(sql);
            ps.setInt(1, userId);
            ResultSet resultSet = ps.executeQuery();

            List<Address> list = new ArrayList<>();
            while (resultSet.next()) {
                int id = resultSet.getInt("id");
                String address = resultSet.getString("address");
                int postalCode = resultSet.getInt("postalcode");
                String  conName = resultSet.getString("conname");
                String conPhone = resultSet.getString("conphone");
                list.add(new Address(id, null, address, postalCode, conName, conPhone));
            }
            return list;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public Address getAddress(Integer id) {
        try {
            Connection connection = JDBCUtil.getConnection();
            String sql = "select * from t_address where id = ? and del = 0";
            PreparedStatement ps = connection.prepareStatement(sql);
            ps.setInt(1, id);
            ResultSet resultSet = ps.executeQuery();

            while (resultSet.next()) {
                String address = resultSet.getString("address");
                int postalCode = resultSet.getInt("postalcode");
                String  conName = resultSet.getString("conname");
                String conPhone = resultSet.getString("conphone");
                return new Address(id, null, address, postalCode, conName, conPhone);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public void updateAddressFirst(Integer id, Integer userId) {
        try {
            Connection connection = JDBCUtil.getConnection();
            String sql = "update t_address set state = 0 where user_id = ?";
            PreparedStatement ps = connection.prepareStatement(sql);
            ps.setInt(1, userId);
            ps.execute();

            sql = "update t_address set state = 1 where id = ?";
            ps = connection.prepareStatement(sql);
            ps.setInt(1, id);
            ps.execute();

            ps.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}
